#!/bin/bash
set -Eeuo pipefail

#待扫描目录，多个目录按空格隔开，例如：“/aa /bb”
target_path="/app_home_dir"
ignore='log|logs|.ssh|nohup.out|.bak|.swp|history|2021'

apphome_conf='conf/app_home.conf'

if [[ $# -eq 1 ]] && [[ "$1" = "-i" ]]
then
    MODIFY=1
else
    MODIFY=0
fi

function get_directorys
{
    if [[ -f ${apphome_conf} ]]
    then
        sed '/^#/d;/^$/d;s/#.*//' ${apphome_conf}
    else
        find ${target_path} -maxdepth 1 -type d -user ${USER}  | egrep -v "${ignore}"
    fi
}

function backup_app
{
   
    echo "  cp -ra $1  ${1}_$(date +%Y%m%d)"
    if [[ ${MODIFY} -eq 1 ]]
    then
        set -x
        cp -ra $1  ${1}_$(date +%Y%m%d)
        set +x
    fi
    
}


function work
{
    set -x
    app_home=$(get_directorys)
    set +x
    for _dir in ${app_home}
    do
        if [ ! -d "${_dir}" ]
        then
            echo "该${_dir}目录不存在，请检查！"
            exit 1
        fi
        echo "1.应用目录......"
        if [ -n "${_dir}" ]
        then
            echo "  **************待备份目录****************"
            echo "  ${_dir}"
            echo " "
            echo "2.应用备份......"
            echo "  ***************备份目录*****************"
            backup_app "${_dir}"
        else
            echo "   未获取到应用目录"
        fi
        done        
}

function main
{    
    work    
}

main
